package com.mlethe.library.socialtool;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.alipay.share.sdk.openapi.IAPAPIEventHandler;
import com.mlethe.library.socialtool.callback.ShareActionListener;
import com.mlethe.library.socialtool.core.Alipay;
import com.mlethe.library.socialtool.core.AlipayShare;
import com.mlethe.library.socialtool.core.Platform;
import com.mlethe.library.socialtool.core.QQ;
import com.mlethe.library.socialtool.core.QQShare;
import com.mlethe.library.socialtool.core.UnionPay;
import com.mlethe.library.socialtool.core.Wechat;
import com.mlethe.library.socialtool.core.WechatShare;
import com.mlethe.library.socialtool.core.Weibo;
import com.mlethe.library.socialtool.core.WeiboShare;
import com.mlethe.library.socialtool.entity.Share;
import com.mlethe.library.socialtool.entity.ShareAudio;
import com.mlethe.library.socialtool.entity.ShareImage;
import com.mlethe.library.socialtool.entity.ShareMiniProgram;
import com.mlethe.library.socialtool.entity.ShareText;
import com.mlethe.library.socialtool.entity.ShareVideo;
import com.mlethe.library.socialtool.entity.ShareWeb;
import com.tencent.mm.opensdk.modelbase.BaseResp;

import java.util.List;

/**
 * 第三方登录、支付和分享工具管理类
 *
 * @author Mlethe
 */
public class SocialTool {

    private Context mContext;

    private Alipay mAlipay;
    private Wechat mWechat;
    private QQ mQQ;
    private Weibo mWeibo;
    private UnionPay mUnionPay;

    private SocialTool() {
    }

    private static final class Holder {
        private static final SocialTool INSTANCE = new SocialTool();
    }

    public static SocialTool getInstance() {
        return Holder.INSTANCE;
    }

    public static SocialTool init(Context context) {
        SocialTool socialTool = getInstance();
        if (context != null) {
            socialTool.mContext = context.getApplicationContext();
        } else {
            socialTool.mContext = null;
        }
        return socialTool;
    }

    /**
     * 微信初始化
     *
     * @param appid  微信 AppId
     * @param secret 微信 secret(不需要本地获取用户信息时不需要secret)
     */
    public SocialTool setWechat(String appid, String secret) {
        if (mWechat == null) {
            mWechat = new Wechat(mContext, appid, secret);
        }
        return this;
    }

    /**
     * 支付宝初始化
     *
     * @param appid       支付宝 AppId
     * @param pid         支付宝账户登录授权业务的入参 pid
     * @param rsa2Private pkcs8 格式的商户私钥
     * @return
     */
    public SocialTool setAlipay(String appid, String pid, String rsa2Private) {
        if (mAlipay == null) {
            mAlipay = new Alipay(mContext, appid, pid, rsa2Private);
        }
        return this;
    }

    /**
     * 支付宝初始化
     *
     * @param appid 支付宝 AppId
     * @return
     */
    public SocialTool setAlipay(String appid) {
        setAlipay(appid, null, null);
        return this;
    }

    /**
     * QQ初始化
     *
     * @param appid QQ appid
     * @return
     */
    public SocialTool setQQ(String appid) {
        if (mQQ == null) {
            mQQ = new QQ(mContext, appid);
        }
        return this;
    }

    /**
     * 微博初始化
     *
     * @param appKey      新浪微博 appKey
     * @param redirectUrl 当前应用的回调页，第三方应用可以使用自己的回调页
     *                    建议使用默认回调页：https://api.weibo.com/oauth2/default.html
     * @return
     */
    public SocialTool setWeibo(String appKey, String redirectUrl) {
        if (mWeibo == null) {
            mWeibo = new Weibo(mContext, appKey, redirectUrl);
        }
        return this;
    }

    /**
     * 中国银联（云闪付）初始化
     *
     * @param appId  中国银联appId
     * @param planid 用于进行业务流程的 Id
     * @return
     */
    public SocialTool setUnionPay(String appId, String planid) {
        if (mUnionPay == null) {
            mUnionPay = new UnionPay(mContext, appId, planid);
        }
        return this;
    }

    /**
     * 中国银联（云闪付）初始化
     *
     * @param appId 中国银联appId
     * @return
     */
    public SocialTool setUnionPay(String appId) {
        setUnionPay(appId, null);
        return this;
    }

    /**
     * 中国银联（云闪付）初始化(只接入支付可用该方法)
     *
     * @return
     */
    public SocialTool setUnionPay() {
        setUnionPay(null, null);
        return this;
    }

    public static Alipay getAlipay() {
        return getInstance().mAlipay;
    }

    public static Wechat getWechat() {
        return getInstance().mWechat;
    }

    public static QQ getQQ() {
        return getInstance().mQQ;
    }

    public static Weibo getWeibo() {
        return getInstance().mWeibo;
    }

    public static UnionPay getUnionPay() {
        return getInstance().mUnionPay;
    }

    /**
     * 微信分享
     *
     * @param share    文本-ShareText、网页-ShareWeb、图片-ShareImage、音乐-ShareAudio、视频-ShareVideo、小程序-ShareMiniProgram
     * @param flag     Share.FLAG_FRIEND：分享到微信好友,Share.FLAG_MOMENTS：分享到微信朋友圈,Share.FLAG_COLLECTION：分享到收藏
     * @param listener
     */
    public final void shareWechat(Share share, int flag, ShareActionListener listener) {
        share(null, share, Platform.WECHAT, flag, listener);
    }

    /**
     * 微信分享小程序
     *
     * @param share    小程序-ShareMiniProgram
     * @param listener
     */
    public final void shareWechat(ShareMiniProgram share, ShareActionListener listener) {
        shareWechat(share, -1, listener);
    }

    /**
     * 支付宝分享
     *
     * @param share 文本-ShareText、网页-ShareWeb、图片-ShareImage
     * @param flag  Share.FLAG_FRIEND：分享到好友，Share.FLAG_MOMENTS：分享到生活圈
     */
    public final void shareAlipay(Share share, int flag, ShareActionListener listener) {
        share(null, share, Platform.ALIPAY, flag, listener);
    }

    /**
     * 支付宝分享(支付宝9.9.5版本及以上 或 分享文本)
     *
     * @param share 文本-ShareText、网页-ShareWeb、图片-ShareImage
     */
    public final void shareAlipay(Share share, ShareActionListener listener) {
        shareAlipay(share, -1, listener);
    }

    /**
     * 支付宝分享结果处理(在 ShareEntryActivity.onCreate() 方法中调用)
     *
     * @param intent
     * @return
     */
    public final boolean handleIntent(Intent intent) {
        return AlipayShare.handleIntent(mAlipay, intent);
    }

    /**
     * QQ分享
     *
     * @param activity activity
     * @param share    网页-ShareWeb、图片-ShareImage、音乐-ShareAudio、小程序-ShareMiniProgram
     * @param listener listener
     */
    public final void shareQQ(Activity activity, Share share, ShareActionListener listener) {
        share(activity, share, Platform.QQ, -1, listener);
    }

    /**
     * 微博初始化
     *
     * @param activity
     */
    public final void initWeibo(Activity activity) {
        mWeibo.init(activity);
    }

    /**
     * 微博分享
     * 重要：请先初始化分享即调用shareWeiboInit(Activity)方法
     * 重要：发起分享的 Activity 必须重写 onActivityResult 调用weiboCallback()方法。
     *
     * @param share 文本-ShareText、网页-ShareWeb、图片-ShareImage、视频-ShareVideo
     */
    public final void shareWeibo(Share share, ShareActionListener listener) {
        share(null, share, Platform.WEIBO, -1, listener);
    }

    /**
     * 微博分享多图
     *
     * @param paths 图片集合
     * @param share 文本-ShareText、网页-ShareWeb、图片-ShareImage、视频-ShareVideo
     */
    public final void shareWeibo(List<String> paths, ShareActionListener listener, Share... share) {
        WeiboShare.setShareActionListener(listener);
        WeiboShare.share(mContext, mWeibo, paths, share);
    }

    /**
     * 微博分享多图
     *
     * @param paths 图片集合
     */
    public final void shareWeibo(List<String> paths, ShareActionListener listener) {
        shareWeibo(paths, listener, null);
    }

    /**
     * 分享回调
     * 重要：发起分享的 Activity 必须重写 onActivityResult
     *
     * @param data data
     */
    public final void onActivityResult(Intent data) {
        WeiboShare.onActivityResult(mWeibo, data);
    }

    /**
     * 分享
     *
     * @param activity activity
     * @param share    share
     * @param platform platform
     * @param flag     flag
     * @param listener listener
     */
    private void share(Activity activity, Share share, int platform, int flag, ShareActionListener listener) {
        if (mContext == null) {
            return;
        }
        if (share instanceof ShareText) {
            /**
             * 分享文本（微信、支付宝、微博）
             */
            ShareText shareText = (ShareText) share;
            if (platform == Platform.WECHAT) {
                WechatShare.setShareActionListener(listener);
                WechatShare.share(mWechat, flag, shareText);
            } else if (platform == Platform.ALIPAY) {
                AlipayShare.setShareActionListener(listener);
                AlipayShare.share(mAlipay, shareText);
            } else if (platform == Platform.WEIBO) {
                WeiboShare.setShareActionListener(listener);
                WeiboShare.share(mContext, mWeibo, shareText);
            }
        } else if (share instanceof ShareWeb) {
            /**
             * 分享网页（微信、支付宝、QQ、微博）
             */
            ShareWeb shareWeb = (ShareWeb) share;
            if (platform == Platform.WECHAT) {
                WechatShare.setShareActionListener(listener);
                WechatShare.share(mContext, mWechat, flag, shareWeb);
            } else if (platform == Platform.ALIPAY) {
                AlipayShare.setShareActionListener(listener);
                AlipayShare.share(mContext, mAlipay, flag, shareWeb);
            } else if (platform == Platform.QQ) {
                QQShare.setShareActionListener(listener);
                QQShare.share(mQQ, shareWeb, activity);
            } else if (platform == Platform.WEIBO) {
                WeiboShare.setShareActionListener(listener);
                WeiboShare.share(mContext, mWeibo, shareWeb);
            }
        } else if (share instanceof ShareImage) {
            /**
             * 分享图片（微信、支付宝、QQ、微博）
             */
            ShareImage shareImage = (ShareImage) share;
            if (platform == Platform.WECHAT) {
                WechatShare.setShareActionListener(listener);
                WechatShare.share(mContext, mWechat, flag, shareImage);
            } else if (platform == Platform.ALIPAY) {
                AlipayShare.setShareActionListener(listener);
                AlipayShare.share(mContext, mAlipay, flag, shareImage);
            } else if (platform == Platform.QQ) {
                QQShare.setShareActionListener(listener);
                QQShare.share(mQQ, shareImage, activity);
            } else if (platform == Platform.WEIBO) {
                WeiboShare.setShareActionListener(listener);
                WeiboShare.share(mContext, mWeibo, shareImage);
            }
        } else if (share instanceof ShareAudio) {
            /**
             * 分享音乐（微信、QQ）
             */
            ShareAudio shareAudio = (ShareAudio) share;
            if (platform == Platform.WECHAT) {
                WechatShare.setShareActionListener(listener);
                WechatShare.share(mContext, mWechat, flag, shareAudio);
            } else if (platform == Platform.QQ) {
                QQShare.setShareActionListener(listener);
                QQShare.share(mQQ, shareAudio, activity);
            }
        } else if (share instanceof ShareVideo) {
            /**
             * 分享视频（微信、微博）
             */
            ShareVideo shareVideo = (ShareVideo) share;
            if (platform == Platform.WECHAT) {
                WechatShare.setShareActionListener(listener);
                WechatShare.share(mContext, mWechat, flag, shareVideo);
            } else if (platform == Platform.WEIBO) {
                WeiboShare.setShareActionListener(listener);
                WeiboShare.share(mContext, mWeibo, shareVideo);
            }
        } else if (share instanceof ShareMiniProgram) {
            /**
             * 分享小程序（微信、QQ）
             */
            ShareMiniProgram shareMiniProgram = (ShareMiniProgram) share;
            if (platform == Platform.WECHAT) {
                WechatShare.setShareActionListener(listener);
                WechatShare.share(mContext, mWechat, shareMiniProgram);
            } else if (platform == Platform.QQ) {
                QQShare.setShareActionListener(listener);
                QQShare.share(mQQ, shareMiniProgram, activity);
            }
        }
    }

    /**
     * 释放内存
     */
    public static void release() {
        SocialTool socialTool = getInstance();
        if (socialTool.mWechat != null) {
            socialTool.mWechat.release();
            socialTool.mWechat = null;
        }
        if (socialTool.mAlipay != null) {
            socialTool.mAlipay.release();
            socialTool.mAlipay = null;
        }
        if (socialTool.mQQ != null) {
            socialTool.mQQ.release();
            socialTool.mQQ = null;
        }
        if (socialTool.mWeibo != null) {
            socialTool.mWeibo.release();
            socialTool.mWeibo = null;
        }
        socialTool.mContext = null;
    }
}
